package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQueryObject;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 服务项目服务impl
 *
 * @author RNIZ
 * @date 2021/11/13
 */
@Service
@Transactional
public class StatementServiceImpl implements IStatementService {
    
    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private AppointmentServiceImpl appointmentService;
    @Autowired
    private IStatementItemService statementItemService;
    @Override
    public TablePageInfo<Statement> query(StatementQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void save(Statement statement) {
        statement.setStatus(Statement.STATUS_CONSUME);
        statementMapper.insert(statement);
    }
    @Override
    @Transactional
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
            statementItemService.deleteByStatementId(dictId);
        }
    }

    @Override
    public void updateBy(BigDecimal totalAmount, BigDecimal totalCount, BigDecimal itemPrice, Long statementId) {
        statementMapper.updateBy(totalAmount,totalCount,itemPrice,statementId);
    }

    @Override
    public void changeBystatus(Long statementId) {
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            throw new BusinessException("订单已经支付");
        }
        statementMapper.changeBystatus(statementId,Statement.STATUS_PAID, ShiroUtils.getUserId());
        if (statement.getAppointmentId()!=null){
            appointmentService.changeByStatus(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
        }

    }

    @Override
    public Statement getByAppipontId(Long id) {
        return statementMapper.selectByAppipontId(id);
    }



    @Override
    public void update(Statement statement) {
        Statement oldstatment = this.get(statement.getId());
        if (Statement.STATUS_PAID.equals(oldstatment.getStatus())){
            throw new BusinessException("订单处于已经支付状态");
        }
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }


}
